package com.jsbs.iam.user.controller;

import com.github.pagehelper.PageInfo;
import com.jsbs.iam.common.core.dto.Result;
import com.jsbs.iam.common.core.utils.Json;
import com.jsbs.iam.user.constant.Constants;
import com.jsbs.iam.user.dto.*;
import com.jsbs.iam.user.entity.IamUserTypeBase;
import com.jsbs.iam.user.service.UserTypeService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 用户类型
 * @BelongsProject: iam
 * @BelongsPackage: com.jsbs.iam.user.controller
 * @Author: WangJiYuan
 * @Description: TODO
 * @Version: 2022-12-20  17:29
 */
@Slf4j
@RestController
@RequestMapping("/iam/user/userType")
public class UserTypeController extends BaseController{

    private static final Logger LOGGER = LoggerFactory.getLogger(UserTypeController.class);

    @Autowired
    private UserTypeService userTypeService;

    /**
     * 查询用户类型数据
     *
     * @param httpServletRequest
     * @return
     */
    @PostMapping("/getUserTypeData")
    public Result getUserTypeData(HttpServletRequest httpServletRequest, @RequestParam("userTypeInfo") String userTypeInfo) {
        log.info("Route=>UserTypeController method=>getUserTypeData 查询用户类型数据");
        try {
            // todo  wjy  临时获取companyCode方法
            String companyCode = httpServletRequest.getHeader(Constants.COMPANY_CODE);
            companyCode = "000001";
            if (StringUtils.isEmpty(companyCode)) {
                log.warn("Route=>UserTypeController method=>getUserTypeData 查询用户类型数据，当前登录人归属租户信息异常");
                return Result.failWithMsg("登录人归属租户信息异常");
            }
            List<IamUserTypeBase> info = userTypeService.getUserTypeData(companyCode, userTypeInfo);
            return new Result(Result.SUCCESS, "查询组织类型成功", info);
        } catch (Exception e) {
            LOGGER.error("Route==>UserTypeController method=>getUserTypeData 查询用户类型数据异常，入参：{}，异常：{}", "", e);
            return handleException(e);
        }
    }

    /**
     * 新增用户类型
     * @param userTypeAddDto
     * @return
     */
    @PostMapping(value = "/addUserType")
    public Result addUserType(HttpServletRequest httpServletRequest, @RequestBody UserTypeAddDto userTypeAddDto) {
        log.info("Route=>UserTypeController method=>addUserType 新增用户类型：{}", Json.toJsonString(userTypeAddDto));
        try {
            // todo  wjy  临时获取companyCode和userCode方法
            String companyCode = httpServletRequest.getHeader(Constants.COMPANY_CODE);
            //String userCode = httpServletRequest.getHeader(Constants.USER_CODE);
            String userName = httpServletRequest.getHeader(Constants.NAME);
            companyCode = "000001";
            //userCode = "001001";
            userName = "wjy";

            if (StringUtils.isEmpty(companyCode)) {
                log.warn("Route=>UserTypeController method=>addUserType 新增用户类型，当前登录人归属租户信息异常");
                return Result.failWithMsg("登录人归属租户信息异常");
            }
            /*if (StringUtils.isEmpty(userCode)) {
                log.warn("Route=>UserTypeController method=>addUserType 新增用户类型，当前登录人信息异常");
                return Result.failWithMsg("登录人信息异常");
            }*/
            if (StringUtils.isEmpty(userName)) {
                log.warn("Route=>UserTypeController method=>addUserType 新增用户类型，当前登录人信息异常");
                return Result.failWithMsg("登录人信息异常");
            }
            //userTypeAddDto.setCompanyCode(companyCode);
            userTypeAddDto.setCreatePerson(userName);

            if (StringUtils.isEmpty(userTypeAddDto.getUserTypeInfo())) {
                log.warn("新增用户类型失败，参数非法{}", userTypeAddDto);
                return Result.failWithMsg("用户类型名称为空,添加失败");
            } else {
                //校验用户类型名称是否唯一
                int count = userTypeService.checkName(userTypeAddDto.getUserTypeInfo(),companyCode);
                if (count > 0) {
                    log.warn("新增用户类型失败，参数非法{}", userTypeAddDto);
                    return Result.failWithMsg("用户类型不唯一,添加失败");
                }
            }
            userTypeAddDto.setCompanyCode(companyCode);
            userTypeService.addUserType(userTypeAddDto);
            return Result.success("添加成功");
        } catch (Exception e) {
            LOGGER.error("Route==>UserTypeController method=>addUserType 新增用户类型异常，入参：{}，异常：{}", Json.toJsonString(userTypeAddDto), e);
            return handleException(e);
        }
    }

    /**
     * 根据用户类型编码逻辑删除用户类型数据
     * @param deleteDto
     * @return
     */
    @PostMapping("/deleteUserType")
    public Result deleteUserType(HttpServletRequest httpServletRequest, @RequestBody UserTypeDeleteDto deleteDto) {
        log.info("Route=>UserTypeController method=>deleteUserType 根据用户类型编码逻辑删除用户类型数据：{}", Json.toJsonString(deleteDto));
        try {
            // todo  wjy  临时获取companyCode和userCode方法
            String companyCode = httpServletRequest.getHeader(Constants.COMPANY_CODE);
            //String userCode = httpServletRequest.getHeader(Constants.USER_CODE);
            String userName = httpServletRequest.getHeader(Constants.NAME);
            companyCode = "000001";
            //userCode = "001001";
            userName = "wjy";

            if (StringUtils.isEmpty(companyCode)) {
                log.warn("Route=>UserTypeController method=>deleteUserType 根据用户类型编码逻辑删除用户类型数据，当前登录人归属租户信息异常");
                return Result.failWithMsg("登录人归属租户信息异常");
            }
            /*if (StringUtils.isEmpty(userCode)) {
                log.warn("Route=>UserTypeController method=>deleteUserType 根据用户类型编码逻辑删除用户类型数据，当前登录人信息异常");
                return Result.failWithMsg("登录人信息异常");
            }*/
            if (StringUtils.isEmpty(userName)) {
                log.warn("Route=>UserTypeController method=>deleteUserType 根据用户类型编码逻辑删除用户类型数据，当前登录人信息异常");
                return Result.failWithMsg("登录人信息异常");
            }
            deleteDto.setCompanyCode(companyCode);
            deleteDto.setUpdatePerson(userName);

            if (StringUtils.isEmpty(deleteDto.getUserTypeCode())) {
                log.warn("根据用户类型编码逻辑删除用户类型数据，参数非法{}", deleteDto.getUserTypeCode());
                return new Result(Result.FAIL, "没有用户类型编码,无法删除");
            }
            //校验用户类型下是否有用户
            int userCount = userTypeService.checkUserCount(deleteDto.getUserTypeCode(), companyCode);
            if (userCount > 0) {
                log.warn("该用户类型下存在用户，不能删除", Json.toJsonString(deleteDto));
                return Result.failWithMsg("该用户类型下存在用户，不能删除");
            }
            //根据用户类型编码逻辑删除用户类型
            userTypeService.deleteUserType(deleteDto);
            return new Result<>(Result.SUCCESS, "删除成功");
        } catch (Exception e) {
            LOGGER.error("Route==>UserTypeController method=>deleteUserType 根据用户类型编码逻辑删除用户类型数据异常，入参：{}，异常：{}", Json.toJsonString(deleteDto), e);
            return handleException(e);
        }
    }

    /**
     * 查询用户类型下的所有字段
     * @param userTypeFieldSelectDto
     * @return
     */
    @PostMapping("/selectUserTypeFieldList")
    public Result selectUserTypeFieldList(HttpServletRequest httpServletRequest, @RequestBody UserTypeFieldSelectDto userTypeFieldSelectDto) {
        log.info("Route=>UserTypeController method=>selectUserTypeFieldList 查询用户类型下的所有字段：{}", Json.toJsonString(userTypeFieldSelectDto));
        try {
            // todo  wjy  临时获取companyCode方法
            String companyCode = httpServletRequest.getHeader(Constants.COMPANY_CODE);
            companyCode = "000001";
            if (StringUtils.isEmpty(companyCode)) {
                log.warn("Route=>UserTypeController method=>selectUserTypeFieldList 查询用户类型下的所有字段，当前登录人归属租户信息异常");
                return Result.failWithMsg("登录人归属租户信息异常");
            }
            userTypeFieldSelectDto.setCompanyCode(companyCode);

            if (StringUtils.isEmpty(userTypeFieldSelectDto.getUserTypeCode())) {
                log.warn("查询用户类型下的所有字段，参数非法{}", userTypeFieldSelectDto.getUserTypeCode());
                return new Result(Result.FAIL, "没有用户类型编码,查询出错");
            }
            //查询对应用户类型下所有字段(分页)
            PageInfo list = userTypeService.selectUserTypeFieldList(userTypeFieldSelectDto);
            return new Result(Result.SUCCESS, "查询成功", list);
        } catch (Exception e) {
            LOGGER.error("Route==>UserTypeController method=>selectUserTypeFieldList 查询用户类型下的所有字段异常，入参：{}，异常：{}", Json.toJsonString(userTypeFieldSelectDto), e);
            return handleException(e);
        }
    }

    /**
     * 新增用户类型字段数据
     * @param userTypeFieldAddDto
     * @return
     */
    @PostMapping("/addUserTypeField")
    public Result addUserTypeField(HttpServletRequest httpServletRequest, @RequestBody UserTypeFieldAddDto userTypeFieldAddDto) {
        log.info("Route=>UserTypeController method=>addUserTypeField 新增用户类型字段数据：{}", Json.toJsonString(userTypeFieldAddDto));
        try {
            // todo  wjy  临时获取companyCode和userCode方法
            String companyCode = httpServletRequest.getHeader(Constants.COMPANY_CODE);
            //String userCode = httpServletRequest.getHeader(Constants.USER_CODE);
            String userName = httpServletRequest.getHeader(Constants.NAME);
            companyCode = "000001";
            //userCode = "001001";
            userName = "wjy";

            if (StringUtils.isEmpty(companyCode)) {
                log.warn("Route=>UserTypeController method=>addUserTypeField 新增用户类型字段数据，当前登录人归属租户信息异常");
                return Result.failWithMsg("登录人归属租户信息异常");
            }
            /*if (StringUtils.isEmpty(userCode)) {
                log.warn("Route=>UserTypeController method=>addUserTypeField 新增用户类型字段数据，当前登录人信息异常");
                return Result.failWithMsg("登录人信息异常");
            }*/
            if (StringUtils.isEmpty(userName)) {
                log.warn("Route=>UserTypeController method=>addUserTypeField 新增用户类型字段数据，当前登录人信息异常");
                return Result.failWithMsg("登录人信息异常");
            }
            userTypeFieldAddDto.setCompanyCode(companyCode);
            userTypeFieldAddDto.setCreatePerson(userName);

            if (StringUtils.isEmpty(userTypeFieldAddDto.getUserTypeCode())) {
                log.warn("新增用户类型字段数据，参数非法{}", userTypeFieldAddDto.getUserTypeCode());
                return new Result(Result.FAIL, "没有用户类型编码,无法添加");
            }
            if (StringUtils.isEmpty(userTypeFieldAddDto.getFieldName())) {
                log.warn("新增用户类型字段数据，参数非法{}", userTypeFieldAddDto.getFieldName());
                return new Result(Result.FAIL, "没有字段名称,无法添加");
            }
            if (StringUtils.isEmpty(userTypeFieldAddDto.getFieldInfo())) {
                log.warn("新增用户类型字段数据，参数非法{}", userTypeFieldAddDto.getFieldInfo());
                return new Result(Result.FAIL, "没有字段描述,无法添加");
            }
            //新增用户类型字段
            userTypeService.addUserTypeField(userTypeFieldAddDto);
            return new Result(Result.SUCCESS, "新增成功");
        } catch (Exception e) {
            LOGGER.error("Route==>UserTypeController method=>addUserTypeField 新增用户类型字段数据异常，入参：{}，异常：{}", Json.toJsonString(userTypeFieldAddDto), e);
            return handleException(e);
        }
    }

    /**
     * 修改用户类型字段
     * @param userTypeFieldUpdateDto
     * @return
     */
    @PostMapping("/updateUserTypeField")
    public Result updateUserTypeField(HttpServletRequest httpServletRequest, @RequestBody UserTypeFieldUpdateDto userTypeFieldUpdateDto) {
        log.info("Route=>UserTypeController method=>updateUserTypeField 修改用户类型字段：{}", Json.toJsonString(userTypeFieldUpdateDto));
        try {
            // todo  wjy  临时获取companyCode和userCode方法
            String companyCode = httpServletRequest.getHeader(Constants.COMPANY_CODE);
            String userCode = httpServletRequest.getHeader(Constants.USER_CODE);
            String userName = httpServletRequest.getHeader(Constants.NAME);
            companyCode = "000001";
            userCode = "001001";
            userName = "wjy";

            if (StringUtils.isEmpty(companyCode)) {
                log.warn("Route=>UserTypeController method=>updateUserTypeField 修改用户类型字段，当前登录人归属租户信息异常");
                return Result.failWithMsg("登录人归属租户信息异常");
            }
            /*if (StringUtils.isEmpty(userCode)) {
                log.warn("Route=>UserTypeController method=>updateUserTypeField 修改用户类型字段，当前登录人信息异常");
                return Result.failWithMsg("登录人信息异常");
            }*/
            if (StringUtils.isEmpty(userName)) {
                log.warn("Route=>UserTypeController method=>updateUserTypeField 修改用户类型字段，当前登录人信息异常");
                return Result.failWithMsg("登录人信息异常");
            }
            userTypeFieldUpdateDto.setCompanyCode(companyCode);
            userTypeFieldUpdateDto.setUpdatePerson(userName);

            if (StringUtils.isEmpty(userTypeFieldUpdateDto.getFieldId())) {
                log.warn("修改用户类型字段，参数非法{}", userTypeFieldUpdateDto.getFieldId());
                return new Result(Result.FAIL, "没有字段id,无法修改");
            }
            //修改用户类型字段
            userTypeService.updateUserTypeField(userTypeFieldUpdateDto);
            return new Result(Result.SUCCESS, "修改成功");
        } catch (Exception e) {
            LOGGER.error("Route==>UserTypeController method=>updateUserTypeField 修改用户类型字段异常，入参：{}，异常：{}", Json.toJsonString(userTypeFieldUpdateDto), e);
            return handleException(e);
        }
    }

    /**
     * 删除用户类型字段(批量)
     * @param userTypeFieldUpdateDto
     * @return
     */
    @PostMapping("/deleteUserTypeField")
    public Result deleteUserTypeField(HttpServletRequest httpServletRequest, @RequestBody UserTypeFieldUpdateDto userTypeFieldUpdateDto) {
        log.info("Route=>UserTypeController method=>deleteUserTypeField 删除用户类型字段(批量)：{}", Json.toJsonString(userTypeFieldUpdateDto));
        try {
            // todo  wjy  临时获取companyCode和userCode方法
            String companyCode = httpServletRequest.getHeader(Constants.COMPANY_CODE);
            //String userCode = httpServletRequest.getHeader(Constants.USER_CODE);
            String userName = httpServletRequest.getHeader(Constants.NAME);
            companyCode = "000001";
            //userCode = "001001";
            userName = "wjy";

            if (StringUtils.isEmpty(companyCode)) {
                log.warn("Route=>UserTypeController method=>deleteUserTypeField 删除用户类型字段，当前登录人归属租户信息异常");
                return Result.failWithMsg("登录人归属租户信息异常");
            }
            /*if (StringUtils.isEmpty(userCode)) {
                log.warn("Route=>UserTypeController method=>deleteUserTypeField 删除用户类型字段，当前登录人信息异常");
                return Result.failWithMsg("登录人信息异常");
            }*/
            if (StringUtils.isEmpty(userName)) {
                log.warn("Route=>UserTypeController method=>deleteUserTypeField 删除用户类型字段，当前登录人信息异常");
                return Result.failWithMsg("登录人信息异常");
            }
            userTypeFieldUpdateDto.setCompanyCode(companyCode);
            userTypeFieldUpdateDto.setUpdatePerson(userName);

            if (CollectionUtils.isEmpty(userTypeFieldUpdateDto.getFieldIds())) {
                log.warn("删除用户类型字段，参数非法{}", userTypeFieldUpdateDto.getFieldIds());
                return new Result(Result.FAIL, "请选择数据再进行删除！");
            }
            //删除用户类型字段(批量)
            userTypeService.deleteUserTypeField(userTypeFieldUpdateDto);
            return new Result(Result.SUCCESS, "删除成功");
        } catch (Exception e) {
            LOGGER.error("Route==>UserTypeController method=>deleteUserTypeField 删除用户类型字段(批量)异常，入参：{}，异常：{}", Json.toJsonString(userTypeFieldUpdateDto), e);
            return handleException(e);
        }
    }



}
